package cn.ww.teach.knowledge.nioKnowledge;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.io.UnsupportedEncodingException;

/**
 * Created by Wangwang on 2017/11/22.
 */
public class ServerReadHandler extends ChannelHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {
        ByteBuf bb = (ByteBuf) msg;
        byte[] bytes = new byte[bb.readableBytes()];
        bb.readBytes(bytes);
        String req = new String(bytes, "utf8");
        String res = "服务端收到，请指示";
        System.out.println("==================>>>>>>>>>>>>>>>Server: 服务端收到 ：" + req);
        ctx.writeAndFlush(Unpooled.copiedBuffer(res.getBytes()));
    }

}
